package com.jbxie.spaceiotmqtt.controller;

import com.jbxie.spaceiotmqtt.mqttUtils.MyMqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/")
public class MqttController {
    @Autowired
    MyMqttClient myMqttClient;

    @GetMapping(value = "/publishTopic")
    public String publishTopic() {
        String topicString = "test";
        myMqttClient.pubMessage(0, false, topicString, "测试一下发布消息");
        return "ok";
    }

    // 发送自定义消息内容（使用默认主题）
    @RequestMapping("/publishTopic/{data}")
    public String test1(@PathVariable("data") String data) {
        String topicString = "test";
        myMqttClient.pubMessage(0,false,topicString, data);
        return "ok";
    }

    // 发送自定义消息内容，且指定主题
    @RequestMapping("/publishTopic/{topic}/{data}")
    public String test2(@PathVariable("topic") String topic, @PathVariable("data") String data) {
        myMqttClient.pubMessage(0,false,topic, data);
        return "ok";
    }

    @RequestMapping("/mqttsub")
    public String mqttsub() {
        String TOPIC1="test_topic1";
        String TOPIC2="test_topic2";
        String TOPIC3="test_topic3";
        String TOPIC4="test_topic4";

        int Qos1=1;
        int Qos2=1;
        int Qos3=1;
        int Qos4=1;

        String[] topics={TOPIC1,TOPIC2,TOPIC3,TOPIC4};
        int[] qos={Qos1,Qos2,Qos3,Qos4};
        myMqttClient.subTopic(topics,qos);
        return "订阅主题";
    }

    @RequestMapping("/mqttest")
    public String mqtest(){
        String TOPIC1="test_topic1";
        String TOPIC2="test_topic2";
        String TOPIC3="test_topic3";
        String TOPIC4="test_topic4";

        String[] topics={TOPIC1,TOPIC2,TOPIC3,TOPIC4};

        myMqttClient.pubMessage(topics[new Random().nextInt(5)], "测试消息发送");
        return "发送成功吗！！！！！！！！";
    }


}
